package org.prothlisberger.musicsearch;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.prothlisberger.musicsearch.db.MusicDatabase;
import org.prothlisberger.musicsearch.exporter.IMusicExporter;
import org.prothlisberger.musicsearch.exporter.PlaylistMusicExporter;
import org.prothlisberger.musicsearch.exporter.XMLExporter;
import org.prothlisberger.musicsearch.exporter.CSVExporter;
@WebServlet("/ExportMusic")
public class ExportMusicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ExportMusicServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String vFormat = request.getParameter("outputType");
		List<File> vFilesToExport = MusicDatabase.getInstance().getAllFiles();
		
		IMusicExporter vExporter = null;	
		
		if (vFormat.equals("PLAYLIST m3u"))
		{
			response.setContentType("audio/x-mpegurl");
			vExporter= new PlaylistMusicExporter();
		}
		
		else if (vFormat.equals("XML"))
		{
			response.setContentType("application/xml");
			vExporter= new XMLExporter();
		}
		
		else if (vFormat.equals("CSV")) 
		{
			response.setContentType("text/csv");
			vExporter= new CSVExporter();
		}

		vExporter.export(vFilesToExport.iterator(), response.getOutputStream());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
